using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Blog.DAL;
using Blog.BAL;
using Blog.web.helpers;
namespace Blog.web.Controllers
{
    public class AjaxController : Controller
    {
        //
        // GET: /Ajax/

        public ActionResult Index()
        {
            return RedirectToAction("","Blog");
        }

        

        public JsonResult get_countries()
        {
            countries ct = new countries();
            
            return Json(ct.get_countries().AsQueryable());
        }
        public JsonResult get_city(int? id)
        {
            cities city = new cities();
            city.con_id = id;
                var a = city.get_cities().AsQueryable();
            return Json(a);
        }
        public JsonResult get_basic_info(int? id)
        {
            member user = new member();
            var u = user.get_mem_info(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult get_interest(int? id)
        {
            member user = new member();
            var u = user.get_interest_info(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult update_interest(string major,string movie,string music,string food,string book,string game,string other)
        {
            member user = new member();
            user.mem_id = Convert.ToInt32(Session["user_id"].ToString());
            user.major =major;
            user.music = music;
            user.food = food;
            user.moive = movie;
            user.game = game;
            user.book = book;
            user.other = other;
            int err = user.update_interested();
            if (err == 0)
            {
                user.Save();
                return Json(1);
            }
            else
                return Json(0);
        }

        public JsonResult get_contact_info(int? id)
        {
            member user = new member();
            var u = user.get_mem_contact(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult get_school_info(int? id)
        {
            member user = new member();
            var u = user.get_school_info(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult get_job_info(int? id)
        {
            member user = new member();
            var u = user.get_job_info(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult update_contact_info(string yahoo, string gmail, string skype)
        {
            member user = new member();
            user.yahoo_id = yahoo;
            user.gmail_id = gmail;
            user.skype_id = skype;
            user.mem_id = Convert.ToInt32(Session["user_id"].ToString());
            int err = user.update_contact();
            if (err == 0)
            {
                user.Save();
                return Json(1);
            }
            else
                return Json(0);
        }

        public JsonResult get_address_info(int? id)
        {
            member user = new member();
            var u = user.get_address_info(Convert.ToInt32(id.ToString())).AsQueryable();
            return Json(u);
        }

        public JsonResult update_school_job_info(string schoolName,string sCountry,string sCity,string level,int yearstart,int yearend,string jCity, string jobname,string company,string jCountry,int jobstart,int jobend,string task)
        {
            member user = new member();
            user.schoolName = schoolName;
            user.sCountry   = sCountry;
            user.sCity      = sCity;
            user.level      = level;
            user.yearStart  = yearstart;
            user.yearEnd    = yearend;
            user.jCity = jCity;
            user.jCountry = jCountry;
            user.jobName = jobname;
            user.task = task;
            user.company = company;
            user.jobStart = jobstart;
            user.jobEnd = jobend;
            user.mem_id = Convert.ToInt32(Session["user_id"].ToString());
            int err = user.update_school();
            if (err == 0)
            {
                user.Save();
                return Json(1);
            }
            else
                return Json(0);
        }

        public JsonResult update_basic_info(string fullname,string birthday,string gender,string country,string city,string email,int day,int month,int year, string about_me)
        {
            member user = new member();
            user.fullname = fullname;
          
            user.sex = gender;
            user.country = (country);
            user.city = (city);
            user.email = (email);
            user.about_me = about_me;
            user.dayJoin = day;
            user.monthJoin = month;
            user.yearJoin = year;
            user.mem_id = Convert.ToInt32(Session["user_id"].ToString());
            int err = user.update();
            if (err == 0)
            {
                user.Save();
                return Json(1);
            }
            else
                return Json(0);
        }

        public JsonResult update_address_info(string address,string country,string city,int livestart,int liveend)
        {
            member user = new member();
            user.aCountry = country;
            user.aCity = city;
            user.liveStart = livestart;
            user.liveEnd = liveend;
            user.mem_id = Convert.ToInt32(Session["user_id"].ToString());
            user.address = address;

            int err = user.update_address();
            if (err == 0)
            {
                user.Save();
                return Json(1);
            }
            else
                return Json(0);
        }
        [ValidateInput(false)]
        public JsonResult add_wall(int mem_id,int friend_id, string action, string message, int blog_id)
        {
            member user = new member(); 
            
            wall w = new wall();
            w.memid = int.Parse(Session["user_id"].ToString());
            w.memid = mem_id;
            w.actions = action;
            w.messages = message;
            w.blogid = blog_id;          
            w.friends_id = friend_id;
            int err = w.add_wall();
            if (err == 0)
            {
                w.Save();
                return Json(1);
            }
            else
                return Json(0);
        }

        public JsonResult delete_wall(int wall_id)
        {
            wall w = new wall();
            w.wall_id = wall_id;
            int err = w.delete_wall();
            if (err == 0)
            {
                w.Save();
                return Json(1);
            }
            else
                return Json(0);

        }

        public JsonResult get_wall(int? mem_id)
        {
            wall w = new wall();
            mem_id = 23001;
            w.memid = Convert.ToInt32(mem_id.ToString());
            var c = w.get_wall().AsQueryable();
            return Json(c);
        }

    }
}
